﻿using System;
using System.Text;
using System.Collections.Generic;
using T.FAS.Runtime.Metadata.Entity.DataModel;

namespace T.FAS.Runtime.DataAccess.CDOEngine
{
    /// <summary>
    /// Select语句分组声明
    /// </summary>
    /// <remarks>类的补充说明</remarks>
    [Serializable]
    public class AggregateStatement : SqlElement
    {
        #region 常量

        /// <summary> 
        /// SelectListStatement 
        /// </summary>
        public const string SELECTLISTSTATEMENT = "AggregateStatement";

        #endregion       

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public AggregateStatement()
        {
            base.CreateChildCollection();
        }

        #endregion       

        #region 方法

        /// <summary>
        /// 将聚合元素转换成SQL，只能用作Select {columns} 的{columns}部分
        /// </summary>
        /// <returns>SQL</returns>
        public override string ToSQL()
        {
            StringBuilder result = new StringBuilder("");
            for (int index = 0; index < this.ChildCollection.Count; index++)
            {
                result.Append(this.ChildCollection[index].ToSQL());
                if (index < this.ChildCollection.Count - 1)
                    result.Append(",");
            }
            
            return result.ToString();
        }
        #endregion
    }

    /// <summary>
    /// Select语句分组元素
    /// </summary>
    /// <remarks>类的补充说明</remarks>
    [Serializable]
    public class AggregateField : Field
    {
        #region 常量

        /// <summary> 
        /// SelectListStatement 
        /// </summary>
        public const string SELECTLISTSTATEMENT = "AggregateField";

        #endregion       

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public AggregateField()
        {
            
        }

        #endregion

        #region Property
        public AggregateType AggregateType { get; set; }

        /// <summary>
        /// 聚合查询列展示在select查询结果中时应用的别名
        /// </summary>
        private string alias = string.Empty;
        /// <summary>
        /// 聚合查询列别名，不允许用户输入防止SQL注入
        /// </summary>
        public string Alias
        {
            get
            {
               
                if (string.IsNullOrEmpty(alias))
                    return $"{AggregateType}_{Table.RealTableName}_{FieldName}";
                else
                    return alias;
            }
            set
            {
                alias = value;
            }
        }

        /// <summary>
        /// CompositeNodeObject.Element.ID
        /// </summary>
        public string CompositeNodeObjectElementID { get; set; }
        /// <summary>
        /// 是否应用count(*)，仅在AggregateType==COUNT时有效
        /// </summary
        public bool IsSelectAll { get; set; }
        #endregion

        #region 方法

        /// <summary>
        /// 转换成SQL
        /// </summary>
        /// <returns>SQL</returns>
        public override string ToSQL()
        {
            if (AggregateType != AggregateType.COUNT)
                return $"{AggregateType}({Table.RealTableName}.{FieldName}) as {Alias}";
            else
            {
                if (IsSelectAll)
                    return $"COUNT(*) as {Alias}";
                else
                    return $"COUNT({Table.RealTableName}.{FieldName}) as {Alias}";
            }
        }

        
        #endregion
    }
}
